import {
  TODO_ADD,
  TODO_DEL_BY_ID,
  TODO_UPDATE_BY_ID,
  TODO_UPDATE_TYPE,
} from '../action/actionTypes';

const initialState = {
  list: [
    { id: 1, isDone: true, task: '学习TS' },
    { id: 2, isDone: true, task: '学习Vue3' },
    { id: 3, isDone: false, task: '学习React' },
  ],
  type: 'all',
};

const todoReducer = (state = initialState, { type, payload }) => {
  console.warn('Logger:  ----->  ', type, payload);
  switch (type) {
    case TODO_DEL_BY_ID:
      return {
        ...state,
        list: state.list.filter((item) => item.id !== payload),
      };
    case TODO_UPDATE_BY_ID:
      return {
        ...state,
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === payload ? !item.isDone : item.isDone,
        })),
      };
    case TODO_ADD:
      return {
        ...state,
        list: [payload, ...state.list],
      };
    case TODO_UPDATE_TYPE:
      return { ...state, type: payload };
    default:
      return state;
  }
};

export default todoReducer;
